import Vue from 'vue';
import {
	Tools,
	Config,
	URL,
	Net,
	VueTools
} from '@/common/tools.js';
Vue.prototype.navigateToSigleUrl = function(url) {
	this.$store.commit('setTabbarIndex', -1);
	uni.navigateTo({
		url: url,
		complete: () => {
			// #ifdef APP-PLUS
			this.Tab.getInstance().show(false);
			// #endif
		}
	});
}

Vue.prototype.isLogin = function() {
	let that = this;
	Net.ayiHttp(
		URL.USER_TOKEN,
		'',
		function(res) {
			console.log('success:', res);
			//已经登陆的处理逻辑
			if (res.code != Config.LOGIN_CODE) {
				VueTools.setStorageSync(Config.IS_LOGIN_STORE_KEY, '1');
			} else {
				VueTools.removeStorageSync(Config.IS_LOGIN_STORE_KEY);
			}
		},
		function(err) {
			console.log('error:', err);
		}
	);
}
Vue.prototype.goBackPage = function() {
	// #ifndef H5
	uni.navigateBack();
	this.Tab.getInstance().show();
	// #endif
	// #ifdef H5
	history.back();
	// #endif
}

/**
 * 获取未结算和今日余额
 * @param {Object} success  成功的回调方法
 */

Vue.prototype.getMoney = function(success) {
	let that = this;
	Net.ayiHttp(
		URL.USER_FINDCREDIT,
		'{}',
		function(res) {
			if (res.code == Config.SUCCESS_CODE) {
				//成功的逻辑
				let data = Tools.aesDecodeToJson(res.data);
				VueTools.setStorageSync(Config.MONEY_KEY, data);
				success(data);
				console.log('获取未结算和今日余额:', data);
			} else if (res.code == Config.FAIL_CODE) {
				VueTools.toastNo(res.message);
			} else if (res.code == Config.LOGIN_CODE) {
				uni.navigateTo({
					url: "/pages/my/login/login"
				});
			}
		},
		function(err) {
			console.log('error:', err);
		}
	);
}


//通过isShowTabMask的事件来进行处理是否显示底部的tabbar-mask的组件
Vue.prototype.isShowTabMask = function() {
	if ((VueTools.getStorageSync(Config.IS_LOGIN_STORE_KEY) == "1" || VueTools.getStorageSync(Config.IS_LOGIN_STORE_KEY) ==
			1)) {
		uni.$emit("isShowTabMask", {
			"isShow": false
		});
	} else {
		uni.$emit("isShowTabMask", {
			"isShow": true
		});
	}
}
/**
 * 判断为空的过滤器
 */
Vue.prototype.empty = function(value) {
	if (value instanceof Array) {
		if (value.length > 0) {
			return false;
		} else {
			return true;
		}
	} else {
		return Tools.isEmpty(value);
	}
}
/**
 * 金额的数据保留小数位
 */
Vue.prototype.decimal = function(value, type) {
	//四舍五入保留两位小数
	if (Tools.isEmpty(value)) {
		return 0;
	}
	if (type == 'three') {
		return Tools.keepThreeDecimal(value);
	} else if (type == 'no') {
		return parseInt(value);
	}
	return Tools.keepTwoDecimal(value);
}
/**
 * 判断是否是正负
 */

Vue.prototype.isnegative = function(value) {
	if (parseFloat(value) < 0) {
		return true;
	} else if (parseFloat(value) > 0) {
		return false;
	}
}


/**
 * 暂时使用的过滤器方法模板使用
 */
Vue.prototype.datestr = function(value, args = '') {
	if (value == 0 || value == '0') {
		return;
	}
	let str = parseInt(value) * 1000; //将php时间戳转化为整形并乘以1000
	let newDate = new Date(str);
	let year = newDate.getUTCFullYear(); //取年份
	let month = newDate.getUTCMonth() + 1; //取月份
	let monthStr = "";
	if (month < 10) {
		monthStr = "0" + month
	} else {
		monthStr = month + "";
	}
	let nowday = newDate.getUTCDate(); //取天数
	let nowdayStr = "";
	if (nowday >= 0 && nowday <= 9) {
		nowdayStr = "0" + nowday;
	} else {
		nowdayStr = nowday + "";
	}
	let hour = newDate.getHours();
	let min = newDate.getMinutes();
	let second = newDate.getSeconds();
	if (args == "t") { //只有时分秒
		return hour + ":" + min + ":" + second;
	} else if (args == "p") { //年月日，时分秒
		return year + "-" + monthStr + "-" + nowdayStr + "  " + hour + ":" + min + ":" + second;
	}
	return year + "-" + monthStr + "-" + nowdayStr; //默认是年月日
}

export default global;
